package com.prp.gwt.server;

import java.util.List;
import java.util.Map;

import com.prp.bo.Administrator;
import com.prp.bo.Entity;
import com.prp.bo.SNSConstants;
import com.prp.db.po.UserInformation;
import com.prp.gwt.client.ClientConstants;
import com.prp.gwt.client.ClientException;
import com.prp.gwt.client.admin.AdminService;
import com.prp.gwt.client.vo.SubjectCategoryList;
import com.prp.gwt.client.vo.UserList;
import com.prp.gwt.client.vo.CommunityList;
import com.prp.gwt.client.vo.User;

public class AdminServiceImpl extends GlobalServiceImpl implements AdminService {

	private static final long serialVersionUID = -1941263030592643678L;

	@Override
	public User getUser() throws ClientException {
		validateAuth(ClientConstants.ROOT);
		return super.updateUser(-1);
	}

	@Override
	public UserList getAdminList() throws ClientException {
		validateAuth(ClientConstants.ROOT);
		return Administrator.getUserListByIdentity(SNSConstants.ADMIN);
	}

	@Override
	public UserList getRootList() throws ClientException {
		validateAuth(ClientConstants.ROOT);
		return Administrator.getUserListByIdentity(SNSConstants.ROOT);

	}

	/**
	 * @author arthur 将管理员的权限设置为普通用户 如果用户不存在返回false 如果用户的权限已经不是管理员,则修改失败
	 *         如果用户尚未激活,返回false
	 * @param user
	 *            指定用户
	 * @return 如果设置成功返回true,否则返回false
	 */
	@Override
	public boolean removeAdmin(String email) throws ClientException {
		validateAuth(ClientConstants.ROOT);
		UserInformation info = com.prp.bo.User.getUser(email);
		if (info == null)
			return false;
		if (!info.getActivated())
			return false;
		if (info.getIdentity() != ClientConstants.ADMIN)
			return false;
		Administrator.setIdentity(info, ClientConstants.USER);
		return true;
	}

	/**
	 * @author arthur 将超级管理员的权限设置为普通用户 如果用户不存在返回false,如果用户的权限已经不是超级管理员,则修改失败
	 * @param user
	 *            指定用户
	 * @return 如果设置成功返回false,否则返回true
	 */
	@Override
	public boolean removeRoot(String email) throws ClientException {
		validateAuth(ClientConstants.ROOT);
		UserInformation info = com.prp.bo.User.getUser(email);
		if (info == null)
			return false;
		if (info.getIdentity() != ClientConstants.ROOT)
			return false;
		if (!info.getActivated())
			return false;
		Administrator.setIdentity(info, ClientConstants.USER);
		return true;
	}

	/**
	 * @author arthur 将帐户的权限设置为管理员 如果用户不存在返回false
	 * @param user
	 *            指定用户
	 * @return 如果设置成功返回false,否则返回true
	 */
	@Override
	public boolean addAdmin(String email) throws ClientException {
		validateAuth(ClientConstants.ROOT);
		UserInformation info = com.prp.bo.User.getUser(email);
		if (info == null)
			return false;
		if (!info.getActivated())
			return false;
		Administrator.setIdentity(info, ClientConstants.ADMIN);
		return true;
	}

	/**
	 * @author arthur 将帐户的权限设置为超级管理员 如果用户不存在返回false
	 * @param user
	 *            指定用户
	 * @return 如果设置成功返回false,否则返回true
	 */
	@Override
	public boolean addRoot(String email) throws ClientException {
		validateAuth(ClientConstants.ROOT);
		UserInformation info = com.prp.bo.User.getUser(email);
		if (info == null)
			return false;
		if (!info.getActivated())
			return false;
		Administrator.setIdentity(info, ClientConstants.ROOT);
		return true;
	}

	@Override
	public CommunityList getCommunityList() throws ClientException {
		validateAuth(ClientConstants.ROOT);
		return Administrator.getCommunityList();
	}

	@Override
	public void divideCommunity() throws ClientException {
		validateAuth(ClientConstants.ROOT);
		Administrator.divideCommunity();
	}

	@Override
	public boolean isDivideRunning() throws ClientException {
		validateAuth(ClientConstants.ROOT);
		return Administrator.isDivideRunning();
	}

	@Override
	public List<String> getEmailSuggest(String query) throws ClientException {
		validateAuth(ClientConstants.ROOT);
		return Administrator.getEmailSuggest(query);
	}

	@Override
	public List<String> getNameSuggest(String query) throws ClientException {
		validateAuth(ClientConstants.ROOT);
		return Administrator.getNameSuggest(query);
	}

	@Override
	public UserList getCommunityMemeber(int communityId) throws ClientException {
		validateAuth(ClientConstants.ROOT);
		return Administrator.getUserListByCommunity(communityId);
	}

	@Override
	public UserList getInactivatedUserList() throws ClientException {
		validateAuth(ClientConstants.ROOT);
		return Administrator.getInactivatedUserList();
	}

	@Override
	public boolean activateUser(String email) throws ClientException {
		validateAuth(ClientConstants.ROOT);
		return Administrator.activateUser(email);
	}

	@Override
	public boolean deleteUser(String email) throws ClientException {
		validateAuth(ClientConstants.ROOT);
		return Administrator.deleteUser(email);
	}

	@Override
	public User searchByEmail(String email) throws ClientException {
		validateAuth(ClientConstants.ROOT);
		if (!com.prp.bo.User.hasUser(email))
			return null;
		UserInformation info = com.prp.bo.User.getUser(email);
		return com.prp.bo.User.parse(info);
	}

	@Override
	public UserList searchByName(String name) throws ClientException {
		validateAuth(ClientConstants.ROOT);
		return Administrator.searchByName(name);
	}
	
	@Override
	public Map<Integer, String> register(User user, String password, String host)
			throws ClientException {
		validateAuth(ClientConstants.ROOT);
		return super.register(user, password, host);
	}

	@Override
	public void updateUserInfo(User user) throws ClientException {
		validateAuth(ClientConstants.ROOT);
		if (!com.prp.bo.User.modifyUserInfo(user.getId(), user))
			throw new ClientException("用户不存在", "找不到该用户，请刷新后再试");
	}

	@Override
	public boolean verify() {
		return super.verify(ClientConstants.ROOT);
	}

	@Override
	public SubjectCategoryList getSubjectCategoryList() throws ClientException {
		return Entity.getSubjectCategoryList();
	}

}
